<template>
  <div>
    <el-table :data="goodsList" style="width: 100%">
      <el-table-column type="index" label="序号" align="center" width="100px" />

      <el-table-column label="图片" align="center">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.img1"
            :zoom-rate="1"
            :preview-src-list="[
              scope.row.img1,
              scope.row.img2,
              scope.row.img3,
              scope.row.img4,
            ]"
            :initial-index="4"
            fit="cover"
            preview-teleported
          />
        </template>
      </el-table-column>

      <el-table-column label="名称" align="center">
        <template #default="scope">
          <div class="text">
            {{ scope.row.proname }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="brand" label="品牌" align="center" />
      <!-- -------------------------筛选------------------------------- -->
      <el-table-column
        align="center"
        prop="category"
        label="分类"
        column-key="date"
        :filters="list.map((item) => ({ text: item, value: item }))"
        :filter-method="filterHandler"
      />
      <!-- -------------------------排序------------------------------- -->
      <el-table-column
        prop="originprice"
        sortable
        label="原价"
        align="center"
      />
      <el-table-column prop="discount" sortable label="折扣" align="center" />
      <el-table-column prop="sales" sortable label="销量" align="center" />
      <!-- -----------------！！！！！！！！！！！---------------------- -->

      <!-- -------------------------操作区------------------------------- -->
      <el-table-column label="是否售卖" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.issale"
            :active-value="1"
            :inactive-value="0"
            @change="handleChange(scope.row.issale, scope.row.proid, 'issale')"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
          />
        </template>
      </el-table-column>

      <el-table-column label="是否秒杀" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isseckill"
            :active-value="1"
            :inactive-value="0"
            @change="
              handleChange(scope.row.isseckill, scope.row.proid, 'isseckill')
            "
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
          />
        </template>
      </el-table-column>

      <el-table-column label="是否推荐" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isrecommend"
            :active-value="1"
            :inactive-value="0"
            @change="
              handleChange(
                scope.row.isrecommend,
                scope.row.proid,
                'isrecommend'
              )
            "
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
          />
        </template>
      </el-table-column>
      <!-- -------------------------删除------------------------------- -->
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" type="danger" @click="handleDelete"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      v-model:current-page="page"
      hide-on-single-page
      :page-size="limitNum"
      @current-change="getGodsList"
    />
  </div>
</template>

<script>
import { Goods, updateFlag } from "../../service/product.js";
export default {
  data() {
    return {
      list: [],
      goodsList: [],
      page: 1,
      limitNum: 10,
      total: 0,
    };
  },
  methods: {
    handleChange(flag, proid, type) {
      updateFlag(flag, proid, type).then((res) => {
        this.getGodsList();
      });
    },
    // 筛选
    filterHandler(value, row, column) {
      const property = column["property"];
      return row[property] === value;
    },
    // 获取数据
    getGodsList() {
      Goods({ count: this.page, limitNum: this.limitNum }).then((res) => {
        this.total = res.total;
        this.goodsList = res.data;
        this.list = [];
        this.goodsList.forEach((item) => {
          this.list.push(item.category);
        });
        // this.list = Array.from(new Set(this.list));
        this.list = [...new Set(this.list)];
      });
    },
    //删除
    handleDelete() {
      console.log(111);
    },
  },

  created() {
    // 获取商品列表数据
    this.getGodsList();
  },
};
</script>

<style scoped>
.el-pagination {
  display: flex;
  justify-content: center;
}
img {
  width: 100%;
}
:deep(.cell) {
  height: 90px;
  line-height: 90px;
}
.text {
  height: 80px;
  line-height: 16px;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 5;
}
</style>
